* This file creates the data required to make Figure D2

set more off

* First create predicted waiting times for each patient to be used later on

"$saveddata/data_complete_withcosts.dta"

gen arrival = 1
gen hour  = arrivalhour
gen date = arrivaldate

forval x=1(1)37{
gen period`x' = 0
}

replace period1 = 1 if depdur>10 & depdur!=.
replace period2 = 1 if depdur>20 & depdur!=.
replace period3 = 1 if depdur>30 & depdur!=.
replace period4 = 1 if depdur>40 & depdur!=.
replace period5 = 1 if depdur>50 & depdur!=.
replace period6 = 1 if depdur>60 & depdur!=.
replace period7 = 1 if depdur>70 & depdur!=.
replace period8 = 1 if depdur>80 & depdur!=.
replace period9 = 1 if depdur>90 & depdur!=.
replace period10 = 1 if depdur>100 & depdur!=.
replace period11 = 1 if depdur>110 & depdur!=.
replace period12 = 1 if depdur>120 & depdur!=.
replace period13 = 1 if depdur>130 & depdur!=.
replace period14 = 1 if depdur>140 & depdur!=.
replace period15 = 1 if depdur>150 & depdur!=.
replace period16 = 1 if depdur>160 & depdur!=.
replace period17 = 1 if depdur>170 & depdur!=.
replace period18 = 1 if depdur>180 & depdur!=.
replace period19 = 1 if depdur>190 & depdur!=.
replace period20 = 1 if depdur>200 & depdur!=.
replace period21 = 1 if depdur>210 & depdur!=.
replace period22 = 1 if depdur>220 & depdur!=.
replace period23 = 1 if depdur>230 & depdur!=.
replace period24 = 1 if depdur>240 & depdur!=.
replace period25 = 1 if depdur>250 & depdur!=.
replace period26 = 1 if depdur>260 & depdur!=.
replace period27 = 1 if depdur>270 & depdur!=.
replace period28 = 1 if depdur>280 & depdur!=.
replace period29 = 1 if depdur>290 & depdur!=.
replace period30 = 1 if depdur>300 & depdur!=.
replace period31 = 1 if depdur>310 & depdur!=.
replace period32 = 1 if depdur>320 & depdur!=.
replace period33 = 1 if depdur>330 & depdur!=.
replace period34 = 1 if depdur>340 & depdur!=.
replace period35 = 1 if depdur>350 & depdur!=.
replace period36 = 1 if depdur>360 & depdur!=.
replace period37 = 1 if depdur>370 & depdur!=.


set more off
foreach x in 1 2 3 4 5 6 {
gen hour`x' = 0
}

replace hour1 = 1 if depdur>60 & depdur!=.
replace hour2 = 1 if depdur>120 & depdur!=.
replace hour3 = 1 if depdur>180 & depdur!=.
replace hour4 = 1 if depdur>240 & depdur!=.
replace hour5 = 1 if depdur>300 & depdur!=.
replace hour6 = 1 if depdur>300 & depdur!=.

drop if depdur>800

reg depdur i.d_age i.d_male i.d_ambulance
predict pred_depdur

gen d1_all = d1
replace d1_all = 40 if d1_all==.

set more off
reg depdur i.d_age i.d_male i.d_ambulance i.d1_all
predict pred_depdur_diag

reghdfe depdur, absorb(i.d_age##i.d1 i.d_male##i.d1 i.d_ambulance##i.d1 i.d1) fast
predict pred_depdur_diag_int

compress
save "$saveddata/predicted_waitingtimes", replace
*/



* Then count TOTAL number of patients who arrive in each period to create the census of those for each period in each hospital
set more off

gen period = 1 if arrivaltime!=. & arrivaltime<10
replace period = 2 if arrivaltime>9 & arrivaltime<20
replace period = 3 if arrivaltime>19 & arrivaltime<30
replace period = 4 if arrivaltime>29 & arrivaltime<40
replace period = 5 if arrivaltime>39 & arrivaltime<50
replace period = 6 if arrivaltime>49 & arrivaltime<100
replace period = 7 if arrivaltime>59 & arrivaltime<110
replace period = 8 if arrivaltime>109 & arrivaltime<120
replace period = 9 if arrivaltime>119 & arrivaltime<130
replace period = 10 if arrivaltime>129 & arrivaltime<140
replace period = 11 if arrivaltime>139 & arrivaltime<150
replace period = 12 if arrivaltime>149 & arrivaltime<200

replace period = 13 if arrivaltime>159 & arrivaltime<210
replace period = 14 if arrivaltime>209 & arrivaltime<220
replace period = 15 if arrivaltime>219 & arrivaltime<230
replace period = 16 if arrivaltime>229 & arrivaltime<240
replace period = 17 if arrivaltime>239 & arrivaltime<250
replace period = 18 if arrivaltime>249 & arrivaltime<300

local x = 3
local z = `x'-1
local y = `x'+1

replace period = 19 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 20 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 21 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 22 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 23 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 24 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 4
local z = `x'-1
local y = `x'+1

replace period = 25 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 26 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 27 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 28 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 29 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 30 if arrivaltime>`x'49 & arrivaltime<`y'00


local x = 5
local z = `x'-1
local y = `x'+1

replace period = 31 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 32 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 33 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 34 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 35 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 36 if arrivaltime>`x'49 & arrivaltime<`y'00


local x = 6
local z = `x'-1
local y = `x'+1

replace period = 37 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 38 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 39 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 40 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 41 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 42 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 7
local z = `x'-1
local y = `x'+1

replace period = 43 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 44 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 45 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 46 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 47 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 48 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 8
local z = `x'-1
local y = `x'+1

replace period = 49 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 50 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 51 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 52 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 53 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 54 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 9
local z = `x'-1
local y = `x'+1

replace period = 55 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 56 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 57 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 58 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 59 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 60 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 10
local z = `x'-1
local y = `x'+1

replace period = 61 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 62 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 63 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 64 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 65 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 66 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 11
local z = `x'-1
local y = `x'+1

replace period = 67 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 68 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 69 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 70 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 71 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 72 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 12
local z = `x'-1
local y = `x'+1

replace period = 73 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 74 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 75 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 76 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 77 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 78 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 13
local z = `x'-1
local y = `x'+1

replace period = 79 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 80 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 81 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 82 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 83 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 84 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 14
local z = `x'-1
local y = `x'+1

replace period = 85 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 86 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 87 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 88 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 89 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 90 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 15
local z = `x'-1
local y = `x'+1

replace period = 91 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 92 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 93 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 94 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 95 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 96 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 16
local z = `x'-1
local y = `x'+1

replace period = 97 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 98 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 99 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 100 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 101 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 102 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 17
local z = `x'-1
local y = `x'+1

replace period = 103 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 104 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 105 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 106 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 107 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 108 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 18
local z = `x'-1
local y = `x'+1

replace period = 109 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 110 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 111 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 112 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 113 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 114 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 19
local z = `x'-1
local y = `x'+1

replace period = 115 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 116 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 117 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 118 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 119 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 120 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 20
local z = `x'-1
local y = `x'+1

replace period = 121 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 122 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 123 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 124 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 125 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 126 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 21
local z = `x'-1
local y = `x'+1

replace period = 127 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 128 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 129 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 130 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 131 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 132 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 22
local z = `x'-1
local y = `x'+1

replace period = 133 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 134 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 135 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 136 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 137 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 138 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 23
local z = `x'-1
local y = `x'+1

replace period = 139 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 140 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 141 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 142 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 143 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 144 if arrivaltime>`x'49 & arrivaltime!=.


collapse (sum) arrival period1 period2 period3 period4 period5 period6 period7 period8 period9 period10 period11 period12 period13 period14 period15 period16 period17 period18 period19 period20 /// 
period21 period22 period23 period24 period25 period26 period27 period28 period29 period30 period31 period32 period33 period34 period35 period36 period37 (mean) depdur, by(period hour date site_code)

save "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", replace
*/


** Then use these arrivals to build a dataset for 4 different groups based on their PPREDICTED times (<150, 1580-180, 180-210, 210-240):

forval i=1(1)4{

if `i'==1{
local start=0
local end=150
}

if `i'==2{
local start=150
local end=180
}

if `i'==3{
local start=180
local end=210
}

if `i'==4{
local start=210
local end=240
}

use "$saveddata/predicted_waitingtimes", clear 

set more off

keep if pred_depdur_diag>=`start' & pred_depdur_diag<`end'


gen period = 1 if arrivaltime!=. & arrivaltime<10
replace period = 2 if arrivaltime>9 & arrivaltime<20
replace period = 3 if arrivaltime>19 & arrivaltime<30
replace period = 4 if arrivaltime>29 & arrivaltime<40
replace period = 5 if arrivaltime>39 & arrivaltime<50
replace period = 6 if arrivaltime>49 & arrivaltime<100
replace period = 7 if arrivaltime>59 & arrivaltime<110
replace period = 8 if arrivaltime>109 & arrivaltime<120
replace period = 9 if arrivaltime>119 & arrivaltime<130
replace period = 10 if arrivaltime>129 & arrivaltime<140
replace period = 11 if arrivaltime>139 & arrivaltime<150
replace period = 12 if arrivaltime>149 & arrivaltime<200

replace period = 13 if arrivaltime>159 & arrivaltime<210
replace period = 14 if arrivaltime>209 & arrivaltime<220
replace period = 15 if arrivaltime>219 & arrivaltime<230
replace period = 16 if arrivaltime>229 & arrivaltime<240
replace period = 17 if arrivaltime>239 & arrivaltime<250
replace period = 18 if arrivaltime>249 & arrivaltime<300

local x = 3
local z = `x'-1
local y = `x'+1

replace period = 19 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 20 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 21 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 22 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 23 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 24 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 4
local z = `x'-1
local y = `x'+1

replace period = 25 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 26 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 27 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 28 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 29 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 30 if arrivaltime>`x'49 & arrivaltime<`y'00


local x = 5
local z = `x'-1
local y = `x'+1

replace period = 31 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 32 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 33 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 34 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 35 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 36 if arrivaltime>`x'49 & arrivaltime<`y'00


local x = 6
local z = `x'-1
local y = `x'+1

replace period = 37 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 38 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 39 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 40 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 41 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 42 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 7
local z = `x'-1
local y = `x'+1

replace period = 43 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 44 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 45 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 46 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 47 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 48 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 8
local z = `x'-1
local y = `x'+1

replace period = 49 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 50 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 51 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 52 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 53 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 54 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 9
local z = `x'-1
local y = `x'+1

replace period = 55 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 56 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 57 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 58 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 59 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 60 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 10
local z = `x'-1
local y = `x'+1

replace period = 61 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 62 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 63 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 64 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 65 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 66 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 11
local z = `x'-1
local y = `x'+1

replace period = 67 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 68 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 69 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 70 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 71 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 72 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 12
local z = `x'-1
local y = `x'+1

replace period = 73 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 74 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 75 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 76 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 77 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 78 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 13
local z = `x'-1
local y = `x'+1

replace period = 79 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 80 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 81 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 82 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 83 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 84 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 14
local z = `x'-1
local y = `x'+1

replace period = 85 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 86 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 87 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 88 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 89 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 90 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 15
local z = `x'-1
local y = `x'+1

replace period = 91 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 92 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 93 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 94 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 95 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 96 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 16
local z = `x'-1
local y = `x'+1

replace period = 97 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 98 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 99 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 100 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 101 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 102 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 17
local z = `x'-1
local y = `x'+1

replace period = 103 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 104 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 105 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 106 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 107 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 108 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 18
local z = `x'-1
local y = `x'+1

replace period = 109 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 110 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 111 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 112 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 113 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 114 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 19
local z = `x'-1
local y = `x'+1

replace period = 115 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 116 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 117 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 118 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 119 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 120 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 20
local z = `x'-1
local y = `x'+1

replace period = 121 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 122 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 123 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 124 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 125 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 126 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 21
local z = `x'-1
local y = `x'+1

replace period = 127 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 128 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 129 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 130 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 131 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 132 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 22
local z = `x'-1
local y = `x'+1

replace period = 133 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 134 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 135 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 136 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 137 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 138 if arrivaltime>`x'49 & arrivaltime<`y'00

local x = 23
local z = `x'-1
local y = `x'+1

replace period = 139 if arrivaltime>`z'59 & arrivaltime<`x'10
replace period = 140 if arrivaltime>`x'09 & arrivaltime<`x'20
replace period = 141 if arrivaltime>`x'19 & arrivaltime<`x'30
replace period = 142 if arrivaltime>`x'29 & arrivaltime<`x'40
replace period = 143 if arrivaltime>`x'39 & arrivaltime<`x'50
replace period = 144 if arrivaltime>`x'49 & arrivaltime!=.


collapse (sum) arrival period1 period2 period3 period4 period5 period6 period7 period8 period9 period10 period11 period12 period13 period14 period15 period16 period17 period18 period19 period20 /// 
period21 period22 period23 period24 period25 period26 period27 period28 period29 period30 period31 period32 period33 period34 period35 period36 period37 (mean) depdur, by(period hour date site_code)

sort site_code date period

set more off
encode site_code, gen(site_num)
keep site_code site_num hour date arrival depdur period

rename hour hour_t0
rename date date_t0
rename arrival arrival_t0
rename depdur depdur_t0
rename period period_t0

gen period = period_t0 - 1
replace period = 144 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==144 & period_t0==1

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period1 arrival depdur)

rename arrival arrival1
rename depdur depdur1
drop if _merge==2
replace period1=0 if period1==.

drop period date _merge

gen period = period_t0 - 2
replace period = 144 if period_t0==2
replace period = 143 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==143 & period_t0==1
replace date = date_t0 - 1 if period==144 & period_t0==2

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period2 arrival depdur)

rename arrival arrival2
rename depdur depdur2
drop if _merge==2
replace period2=0 if period2==.

drop period date _merge

gen period = period_t0 - 3
replace period = 144 if period_t0==3
replace period = 143 if period_t0==2
replace period = 142 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==142 & period_t0==1
replace date = date_t0 - 1 if period==143 & period_t0==2
replace date = date_t0 - 1 if period==144 & period_t0==3

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period3 arrival depdur)

rename arrival arrival3
rename depdur depdur3
drop if _merge==2
replace period3=0 if period3==.

drop period date _merge

gen period = period_t0 - 4
replace period = 144 if period_t0==4
replace period = 143 if period_t0==3
replace period = 142 if period_t0==2
replace period = 141 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==141 & period_t0==1
replace date = date_t0 - 1 if period==142 & period_t0==2
replace date = date_t0 - 1 if period==143 & period_t0==3
replace date = date_t0 - 1 if period==144 & period_t0==4

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period4 arrival depdur)

rename arrival arrival4
rename depdur depdur4
drop if _merge==2
replace period4=0 if period4==.

drop period date _merge

gen period = period_t0 - 5
replace period = 144 if period_t0==5
replace period = 143 if period_t0==4
replace period = 142 if period_t0==3
replace period = 141 if period_t0==2
replace period = 140 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==140 & period_t0==1
replace date = date_t0 - 1 if period==141 & period_t0==2
replace date = date_t0 - 1 if period==142 & period_t0==3
replace date = date_t0 - 1 if period==143 & period_t0==4
replace date = date_t0 - 1 if period==144 & period_t0==5

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period5 arrival depdur)

rename arrival arrival5
rename depdur depdur5
drop if _merge==2
replace period5=0 if period5==.

drop period date _merge

gen period = period_t0 - 6
replace period = 144 if period_t0==6
replace period = 143 if period_t0==5
replace period = 142 if period_t0==4
replace period = 141 if period_t0==3
replace period = 140 if period_t0==2
replace period = 139 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==139 & period_t0==1
replace date = date_t0 - 1 if period==140 & period_t0==2
replace date = date_t0 - 1 if period==141 & period_t0==3
replace date = date_t0 - 1 if period==142 & period_t0==4
replace date = date_t0 - 1 if period==143 & period_t0==5
replace date = date_t0 - 1 if period==144 & period_t0==6

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period6 arrival depdur)

rename arrival arrival6
rename depdur depdur6
drop if _merge==2
replace period6=0 if period6==.

drop period date _merge

gen period = period_t0 - 7
replace period = 144 if period_t0==7
replace period = 143 if period_t0==6
replace period = 142 if period_t0==5
replace period = 141 if period_t0==4
replace period = 140 if period_t0==3
replace period = 139 if period_t0==2
replace period = 138 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==138 & period_t0==1
replace date = date_t0 - 1 if period==139 & period_t0==2
replace date = date_t0 - 1 if period==140 & period_t0==3
replace date = date_t0 - 1 if period==141 & period_t0==4
replace date = date_t0 - 1 if period==142 & period_t0==5
replace date = date_t0 - 1 if period==143 & period_t0==6
replace date = date_t0 - 1 if period==144 & period_t0==7

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period7 arrival depdur)

rename arrival arrival7
rename depdur depdur7
drop if _merge==2

set more off
replace period7=0 if period7==.

drop period date _merge

gen period = period_t0 - 8
replace period = 144 if period_t0==8
replace period = 143 if period_t0==7
replace period = 142 if period_t0==6
replace period = 141 if period_t0==5
replace period = 140 if period_t0==4
replace period = 139 if period_t0==3
replace period = 138 if period_t0==2
replace period = 137 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==137 & period_t0==1
replace date = date_t0 - 1 if period==138 & period_t0==2
replace date = date_t0 - 1 if period==139 & period_t0==3
replace date = date_t0 - 1 if period==140 & period_t0==4
replace date = date_t0 - 1 if period==141 & period_t0==5
replace date = date_t0 - 1 if period==142 & period_t0==6
replace date = date_t0 - 1 if period==143 & period_t0==7
replace date = date_t0 - 1 if period==144 & period_t0==8

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period8 arrival depdur)

rename arrival arrival8
rename depdur depdur8
drop if _merge==2
replace period8=0 if period8==.

drop period date _merge

gen period = period_t0 - 9
replace period = 144 if period_t0==9
replace period = 143 if period_t0==8
replace period = 142 if period_t0==7
replace period = 141 if period_t0==6
replace period = 140 if period_t0==5
replace period = 139 if period_t0==4
replace period = 138 if period_t0==3
replace period = 137 if period_t0==2
replace period = 136 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==136 & period_t0==1
replace date = date_t0 - 1 if period==137 & period_t0==2
replace date = date_t0 - 1 if period==138 & period_t0==3
replace date = date_t0 - 1 if period==139 & period_t0==4
replace date = date_t0 - 1 if period==140 & period_t0==5
replace date = date_t0 - 1 if period==141 & period_t0==6
replace date = date_t0 - 1 if period==142 & period_t0==7
replace date = date_t0 - 1 if period==143 & period_t0==8
replace date = date_t0 - 1 if period==144 & period_t0==9

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period9 arrival depdur)

rename arrival arrival9
rename depdur depdur9
drop if _merge==2
replace period9=0 if period9==.

drop period date _merge

gen period = period_t0 - 10
replace period = 144 if period_t0==10
replace period = 143 if period_t0==9
replace period = 142 if period_t0==8
replace period = 141 if period_t0==7
replace period = 140 if period_t0==6
replace period = 139 if period_t0==5
replace period = 138 if period_t0==4
replace period = 137 if period_t0==3
replace period = 136 if period_t0==2
replace period = 135 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==135 & period_t0==1
replace date = date_t0 - 1 if period==136 & period_t0==2
replace date = date_t0 - 1 if period==137 & period_t0==3
replace date = date_t0 - 1 if period==138 & period_t0==4
replace date = date_t0 - 1 if period==139 & period_t0==5
replace date = date_t0 - 1 if period==140 & period_t0==6
replace date = date_t0 - 1 if period==141 & period_t0==7
replace date = date_t0 - 1 if period==142 & period_t0==8
replace date = date_t0 - 1 if period==143 & period_t0==9
replace date = date_t0 - 1 if period==144 & period_t0==10

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period10 arrival depdur)

rename arrival arrival10
rename depdur depdur10
drop if _merge==2
replace period10=0 if period10==.

drop period date _merge

gen period = period_t0 - 11
replace period = 144 if period_t0==11
replace period = 143 if period_t0==10
replace period = 142 if period_t0==9
replace period = 141 if period_t0==8
replace period = 140 if period_t0==7
replace period = 139 if period_t0==6
replace period = 138 if period_t0==5
replace period = 137 if period_t0==4
replace period = 136 if period_t0==3
replace period = 135 if period_t0==2
replace period = 134 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period11 arrival depdur)

rename arrival arrival11
rename depdur depdur11
drop if _merge==2
replace period11=0 if period11==.

drop period date _merge

gen period = period_t0 - 12
replace period = 144 if period_t0==12
replace period = 143 if period_t0==11
replace period = 142 if period_t0==10
replace period = 141 if period_t0==9
replace period = 140 if period_t0==8
replace period = 139 if period_t0==7
replace period = 138 if period_t0==6
replace period = 137 if period_t0==5
replace period = 136 if period_t0==4
replace period = 135 if period_t0==3
replace period = 134 if period_t0==2
replace period = 133 if period_t0==1
gen date = date_t0
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period12 arrival depdur)

rename arrival arrival12
rename depdur depdur12
drop if _merge==2
replace period12=0 if period12==.

drop period date _merge

gen period = period_t0 - 13
replace period = 144 if period_t0==13
replace period = 143 if period_t0==12
replace period = 142 if period_t0==11
replace period = 141 if period_t0==10
replace period = 140 if period_t0==9
replace period = 139 if period_t0==8
replace period = 138 if period_t0==7
replace period = 137 if period_t0==6
replace period = 136 if period_t0==5
replace period = 135 if period_t0==4
replace period = 134 if period_t0==3
replace period = 133 if period_t0==2
replace period = 132 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period13 arrival depdur)

rename arrival arrival13
rename depdur depdur13
drop if _merge==2
replace period13=0 if period13==.

drop period date _merge

gen period = period_t0 - 14
replace period = 144 if period_t0==14
replace period = 143 if period_t0==13
replace period = 142 if period_t0==12
replace period = 141 if period_t0==11
replace period = 140 if period_t0==10
replace period = 139 if period_t0==9
replace period = 138 if period_t0==8
replace period = 137 if period_t0==7
replace period = 136 if period_t0==6
replace period = 135 if period_t0==5
replace period = 134 if period_t0==4
replace period = 133 if period_t0==3
replace period = 132 if period_t0==2
replace period = 131 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period14 arrival depdur)

rename arrival arrival14
rename depdur depdur14
drop if _merge==2
replace period14=0 if period14==.

drop period date _merge

gen period = period_t0 - 15
replace period = 144 if period_t0==15
replace period = 143 if period_t0==14
replace period = 142 if period_t0==13
replace period = 141 if period_t0==12
replace period = 140 if period_t0==11
replace period = 139 if period_t0==10
replace period = 138 if period_t0==9
replace period = 137 if period_t0==8
replace period = 136 if period_t0==7
replace period = 135 if period_t0==6
replace period = 134 if period_t0==5
replace period = 133 if period_t0==4
replace period = 132 if period_t0==3
replace period = 131 if period_t0==2
replace period = 130 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period15 arrival depdur)

rename arrival arrival15
rename depdur depdur15
drop if _merge==2
replace period15=0 if period15==.

drop period date _merge

gen period = period_t0 - 16
replace period = 144 if period_t0==16
replace period = 143 if period_t0==15
replace period = 142 if period_t0==14
replace period = 141 if period_t0==13
replace period = 140 if period_t0==12
replace period = 139 if period_t0==11
replace period = 138 if period_t0==10
replace period = 137 if period_t0==9
replace period = 136 if period_t0==8
replace period = 135 if period_t0==7
replace period = 134 if period_t0==6
replace period = 133 if period_t0==5
replace period = 132 if period_t0==4
replace period = 131 if period_t0==3
replace period = 130 if period_t0==2
replace period = 129 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period16 arrival depdur)

rename arrival arrival16
rename depdur depdur16
drop if _merge==2
replace period16=0 if period16==.

drop period date _merge

gen period = period_t0 - 17
replace period = 144 if period_t0==17
replace period = 143 if period_t0==16
replace period = 142 if period_t0==15
replace period = 141 if period_t0==14
replace period = 140 if period_t0==13
replace period = 139 if period_t0==12
replace period = 138 if period_t0==11
replace period = 137 if period_t0==10
replace period = 136 if period_t0==9
replace period = 135 if period_t0==8
replace period = 134 if period_t0==7
replace period = 133 if period_t0==6
replace period = 132 if period_t0==5
replace period = 131 if period_t0==4
replace period = 130 if period_t0==3
replace period = 129 if period_t0==2
replace period = 128 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period17 arrival depdur)

rename arrival arrival17
rename depdur depdur17
drop if _merge==2
replace period17=0 if period17==.

drop period date _merge

gen period = period_t0 - 18
replace period = 144 if period_t0==18
replace period = 143 if period_t0==17
replace period = 142 if period_t0==16
replace period = 141 if period_t0==15
replace period = 140 if period_t0==14
replace period = 139 if period_t0==13
replace period = 138 if period_t0==12
replace period = 137 if period_t0==11
replace period = 136 if period_t0==10
replace period = 135 if period_t0==9
replace period = 134 if period_t0==8
replace period = 133 if period_t0==7
replace period = 132 if period_t0==6
replace period = 131 if period_t0==5
replace period = 130 if period_t0==4
replace period = 129 if period_t0==3
replace period = 128 if period_t0==2
replace period = 127 if period_t0==1


gen date = date_t0
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period18 arrival depdur)

rename arrival arrival18
rename depdur depdur18
drop if _merge==2
replace period18=0 if period18==.

drop period date _merge

gen period = period_t0 - 19
replace period = 144 if period_t0==19
replace period = 143 if period_t0==18
replace period = 142 if period_t0==17
replace period = 141 if period_t0==16
replace period = 140 if period_t0==15
replace period = 139 if period_t0==14
replace period = 138 if period_t0==13
replace period = 137 if period_t0==12
replace period = 136 if period_t0==11
replace period = 135 if period_t0==10
replace period = 134 if period_t0==9
replace period = 133 if period_t0==8
replace period = 132 if period_t0==7
replace period = 131 if period_t0==6
replace period = 130 if period_t0==5
replace period = 129 if period_t0==4
replace period = 128 if period_t0==3
replace period = 127 if period_t0==2
replace period = 126 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period19 arrival depdur)

rename arrival arrival19
rename depdur depdur19
drop if _merge==2
replace period19=0 if period19==.

drop period date _merge

gen period = period_t0 - 20
replace period = 144 if period_t0==20
replace period = 143 if period_t0==19
replace period = 142 if period_t0==18
replace period = 141 if period_t0==17
replace period = 140 if period_t0==16
replace period = 139 if period_t0==15
replace period = 138 if period_t0==14
replace period = 137 if period_t0==13
replace period = 136 if period_t0==12
replace period = 135 if period_t0==11
replace period = 134 if period_t0==10
replace period = 133 if period_t0==9
replace period = 132 if period_t0==8
replace period = 131 if period_t0==7
replace period = 130 if period_t0==6
replace period = 129 if period_t0==5
replace period = 128 if period_t0==4
replace period = 127 if period_t0==3
replace period = 126 if period_t0==2
replace period = 125 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period20 arrival depdur)

rename arrival arrival20
rename depdur depdur20
drop if _merge==2
replace period20=0 if period20==.

drop period date _merge

gen period = period_t0 - 21
replace period = 144 if period_t0==21
replace period = 143 if period_t0==20
replace period = 142 if period_t0==19
replace period = 141 if period_t0==18
replace period = 140 if period_t0==17
replace period = 139 if period_t0==16
replace period = 138 if period_t0==15
replace period = 137 if period_t0==14
replace period = 136 if period_t0==13
replace period = 135 if period_t0==12
replace period = 134 if period_t0==11
replace period = 133 if period_t0==10
replace period = 132 if period_t0==9
replace period = 131 if period_t0==8
replace period = 130 if period_t0==7
replace period = 129 if period_t0==6
replace period = 128 if period_t0==5
replace period = 127 if period_t0==4
replace period = 126 if period_t0==3
replace period = 125 if period_t0==2
replace period = 124 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period21 arrival depdur)

rename arrival arrival21
rename depdur depdur21
drop if _merge==2
replace period21=0 if period21==.

drop period date _merge

gen period = period_t0 - 22
replace period = 144 if period_t0==22
replace period = 143 if period_t0==21
replace period = 142 if period_t0==20
replace period = 141 if period_t0==19
replace period = 140 if period_t0==18
replace period = 139 if period_t0==17
replace period = 138 if period_t0==16
replace period = 137 if period_t0==15
replace period = 136 if period_t0==14
replace period = 135 if period_t0==13
replace period = 134 if period_t0==12
replace period = 133 if period_t0==11
replace period = 132 if period_t0==10
replace period = 131 if period_t0==9
replace period = 130 if period_t0==8
replace period = 129 if period_t0==7
replace period = 128 if period_t0==6
replace period = 127 if period_t0==5
replace period = 126 if period_t0==4
replace period = 125 if period_t0==3
replace period = 124 if period_t0==2
replace period = 123 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period22 arrival depdur)

rename arrival arrival22
rename depdur depdur22
drop if _merge==2
replace period22=0 if period22==.

drop period date _merge

gen period = period_t0 - 23
replace period = 144 if period_t0==23
replace period = 143 if period_t0==22
replace period = 142 if period_t0==21
replace period = 141 if period_t0==20
replace period = 140 if period_t0==19
replace period = 139 if period_t0==18
replace period = 138 if period_t0==17
replace period = 137 if period_t0==16
replace period = 136 if period_t0==15
replace period = 135 if period_t0==14
replace period = 134 if period_t0==13
replace period = 133 if period_t0==12
replace period = 132 if period_t0==11
replace period = 131 if period_t0==10
replace period = 130 if period_t0==9
replace period = 129 if period_t0==8
replace period = 128 if period_t0==7
replace period = 127 if period_t0==6
replace period = 126 if period_t0==5
replace period = 125 if period_t0==4
replace period = 124 if period_t0==3
replace period = 123 if period_t0==2
replace period = 122 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period23 arrival depdur)

rename arrival arrival23
rename depdur depdur23
drop if _merge==2
replace period23=0 if period23==.

drop period date _merge

gen period = period_t0 - 24
replace period = 144 if period_t0==24
replace period = 143 if period_t0==23
replace period = 142 if period_t0==22
replace period = 141 if period_t0==21
replace period = 140 if period_t0==20
replace period = 139 if period_t0==19
replace period = 138 if period_t0==18
replace period = 137 if period_t0==17
replace period = 136 if period_t0==16
replace period = 135 if period_t0==15
replace period = 134 if period_t0==14
replace period = 133 if period_t0==13
replace period = 132 if period_t0==12
replace period = 131 if period_t0==11
replace period = 130 if period_t0==10
replace period = 129 if period_t0==9
replace period = 128 if period_t0==8
replace period = 127 if period_t0==7
replace period = 126 if period_t0==6
replace period = 125 if period_t0==5
replace period = 124 if period_t0==4
replace period = 123 if period_t0==3
replace period = 122 if period_t0==2
replace period = 121 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period24 arrival depdur)

rename arrival arrival24
rename depdur depdur24
drop if _merge==2
replace period24=0 if period24==.

drop period date _merge

gen period = period_t0 - 25
replace period = 144 if period_t0==25
replace period = 143 if period_t0==24
replace period = 142 if period_t0==23
replace period = 141 if period_t0==22
replace period = 140 if period_t0==21
replace period = 139 if period_t0==20
replace period = 138 if period_t0==19
replace period = 137 if period_t0==18
replace period = 136 if period_t0==17
replace period = 135 if period_t0==16
replace period = 134 if period_t0==15
replace period = 133 if period_t0==14
replace period = 132 if period_t0==13
replace period = 131 if period_t0==12
replace period = 130 if period_t0==11
replace period = 129 if period_t0==10
replace period = 128 if period_t0==9
replace period = 127 if period_t0==8
replace period = 126 if period_t0==7
replace period = 125 if period_t0==6
replace period = 124 if period_t0==5
replace period = 123 if period_t0==4
replace period = 122 if period_t0==3
replace period = 121 if period_t0==2
replace period = 120 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period25 arrival depdur)

rename arrival arrival25
rename depdur depdur25
drop if _merge==2
replace period25=0 if period25==.

drop period date _merge

gen period = period_t0 - 26
replace period = 144 if period_t0==26
replace period = 143 if period_t0==25
replace period = 142 if period_t0==24
replace period = 141 if period_t0==23
replace period = 140 if period_t0==22
replace period = 139 if period_t0==21
replace period = 138 if period_t0==20
replace period = 137 if period_t0==19
replace period = 136 if period_t0==18
replace period = 135 if period_t0==17
replace period = 134 if period_t0==16
replace period = 133 if period_t0==15
replace period = 132 if period_t0==14
replace period = 131 if period_t0==13
replace period = 130 if period_t0==12
replace period = 129 if period_t0==11
replace period = 128 if period_t0==10
replace period = 127 if period_t0==9
replace period = 126 if period_t0==8
replace period = 125 if period_t0==7
replace period = 124 if period_t0==6
replace period = 123 if period_t0==5
replace period = 122 if period_t0==4
replace period = 121 if period_t0==3
replace period = 120 if period_t0==2
replace period = 119 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period26 arrival depdur)

rename arrival arrival26
rename depdur depdur26
drop if _merge==2
replace period26=0 if period26==.

drop period date _merge

gen period = period_t0 - 27
replace period = 144 if period_t0==27
replace period = 143 if period_t0==26
replace period = 142 if period_t0==25
replace period = 141 if period_t0==24
replace period = 140 if period_t0==23
replace period = 139 if period_t0==22
replace period = 138 if period_t0==21
replace period = 137 if period_t0==20
replace period = 136 if period_t0==19
replace period = 135 if period_t0==18
replace period = 134 if period_t0==17
replace period = 133 if period_t0==16
replace period = 132 if period_t0==15
replace period = 131 if period_t0==14
replace period = 130 if period_t0==13
replace period = 129 if period_t0==12
replace period = 128 if period_t0==11
replace period = 127 if period_t0==10
replace period = 126 if period_t0==9
replace period = 125 if period_t0==8
replace period = 124 if period_t0==7
replace period = 123 if period_t0==6
replace period = 122 if period_t0==5
replace period = 121 if period_t0==4
replace period = 120 if period_t0==3
replace period = 119 if period_t0==2
replace period = 118 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==118
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period27 arrival depdur)

rename arrival arrival27
rename depdur depdur27
drop if _merge==2
replace period27=0 if period27==.

drop period date _merge


gen period = period_t0 - 28
replace period = 144 if period_t0==28
replace period = 143 if period_t0==27
replace period = 142 if period_t0==26
replace period = 141 if period_t0==25
replace period = 140 if period_t0==24
replace period = 139 if period_t0==23
replace period = 138 if period_t0==22
replace period = 137 if period_t0==21
replace period = 136 if period_t0==20
replace period = 135 if period_t0==19
replace period = 134 if period_t0==18
replace period = 133 if period_t0==17
replace period = 132 if period_t0==16
replace period = 131 if period_t0==15
replace period = 130 if period_t0==14
replace period = 129 if period_t0==13
replace period = 128 if period_t0==12
replace period = 127 if period_t0==11
replace period = 126 if period_t0==10
replace period = 125 if period_t0==9
replace period = 124 if period_t0==8
replace period = 123 if period_t0==7
replace period = 122 if period_t0==6
replace period = 121 if period_t0==5
replace period = 120 if period_t0==4
replace period = 119 if period_t0==3
replace period = 118 if period_t0==2
replace period = 117 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==117
replace date = date_t0 - 1 if period==118
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period28 arrival depdur)

rename arrival arrival28
rename depdur depdur28
drop if _merge==2
replace period28=0 if period28==.

drop period date _merge

gen period = period_t0 - 29
replace period = 144 if period_t0==29
replace period = 143 if period_t0==28
replace period = 142 if period_t0==27
replace period = 141 if period_t0==26
replace period = 140 if period_t0==25
replace period = 139 if period_t0==24
replace period = 138 if period_t0==23
replace period = 137 if period_t0==22
replace period = 136 if period_t0==21
replace period = 135 if period_t0==20
replace period = 134 if period_t0==19
replace period = 133 if period_t0==18
replace period = 132 if period_t0==17
replace period = 131 if period_t0==16
replace period = 130 if period_t0==15
replace period = 129 if period_t0==14
replace period = 128 if period_t0==13
replace period = 127 if period_t0==12
replace period = 126 if period_t0==11
replace period = 125 if period_t0==10
replace period = 124 if period_t0==9
replace period = 123 if period_t0==8
replace period = 122 if period_t0==7
replace period = 121 if period_t0==6
replace period = 120 if period_t0==5
replace period = 119 if period_t0==4
replace period = 118 if period_t0==3
replace period = 117 if period_t0==2
replace period = 116 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==116
replace date = date_t0 - 1 if period==117
replace date = date_t0 - 1 if period==118
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period29 arrival depdur)
rename arrival arrival29
rename depdur depdur29
drop if _merge==2
replace period29=0 if period29==.

drop period date _merge

gen period = period_t0 - 30
replace period = 144 if period_t0==30
replace period = 143 if period_t0==29
replace period = 142 if period_t0==28
replace period = 141 if period_t0==27
replace period = 140 if period_t0==26
replace period = 139 if period_t0==25
replace period = 138 if period_t0==24
replace period = 137 if period_t0==23
replace period = 136 if period_t0==22
replace period = 135 if period_t0==21
replace period = 134 if period_t0==20
replace period = 133 if period_t0==19
replace period = 132 if period_t0==18
replace period = 131 if period_t0==17
replace period = 130 if period_t0==16
replace period = 129 if period_t0==15
replace period = 128 if period_t0==14
replace period = 127 if period_t0==13
replace period = 126 if period_t0==12
replace period = 125 if period_t0==11
replace period = 124 if period_t0==10
replace period = 123 if period_t0==9
replace period = 122 if period_t0==8
replace period = 121 if period_t0==7
replace period = 120 if period_t0==6
replace period = 119 if period_t0==5
replace period = 118 if period_t0==4
replace period = 117 if period_t0==3
replace period = 116 if period_t0==2
replace period = 115 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==115
replace date = date_t0 - 1 if period==116
replace date = date_t0 - 1 if period==117
replace date = date_t0 - 1 if period==118
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period30 arrival depdur)
rename arrival arrival30
rename depdur depdur30
drop if _merge==2
replace period30=0 if period30==.

drop period date _merge


gen period = period_t0 - 31
replace period = 144 if period_t0==31
replace period = 143 if period_t0==30
replace period = 142 if period_t0==29
replace period = 141 if period_t0==28
replace period = 140 if period_t0==27
replace period = 139 if period_t0==26
replace period = 138 if period_t0==25
replace period = 137 if period_t0==24
replace period = 136 if period_t0==23
replace period = 135 if period_t0==22
replace period = 134 if period_t0==21
replace period = 133 if period_t0==20
replace period = 132 if period_t0==19
replace period = 131 if period_t0==18
replace period = 130 if period_t0==17
replace period = 129 if period_t0==16
replace period = 128 if period_t0==15
replace period = 127 if period_t0==14
replace period = 126 if period_t0==13
replace period = 125 if period_t0==12
replace period = 124 if period_t0==11
replace period = 123 if period_t0==10
replace period = 122 if period_t0==9
replace period = 121 if period_t0==8
replace period = 120 if period_t0==7
replace period = 119 if period_t0==6
replace period = 118 if period_t0==5
replace period = 117 if period_t0==4
replace period = 116 if period_t0==3
replace period = 115 if period_t0==2
replace period = 114 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==114
replace date = date_t0 - 1 if period==115
replace date = date_t0 - 1 if period==116
replace date = date_t0 - 1 if period==117
replace date = date_t0 - 1 if period==118
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period31 arrival depdur)
rename arrival arrival31
rename depdur depdur31
drop if _merge==2
replace period31=0 if period31==.

drop period date _merge


gen period = period_t0 - 32
replace period = 144 if period_t0==32
replace period = 143 if period_t0==31
replace period = 142 if period_t0==30
replace period = 141 if period_t0==29
replace period = 140 if period_t0==28
replace period = 139 if period_t0==27
replace period = 138 if period_t0==26
replace period = 137 if period_t0==25
replace period = 136 if period_t0==24
replace period = 135 if period_t0==23
replace period = 134 if period_t0==22
replace period = 133 if period_t0==21
replace period = 132 if period_t0==20
replace period = 131 if period_t0==19
replace period = 130 if period_t0==18
replace period = 129 if period_t0==17
replace period = 128 if period_t0==16
replace period = 127 if period_t0==15
replace period = 126 if period_t0==14
replace period = 125 if period_t0==13
replace period = 124 if period_t0==12
replace period = 123 if period_t0==11
replace period = 122 if period_t0==10
replace period = 121 if period_t0==9
replace period = 120 if period_t0==8
replace period = 119 if period_t0==7
replace period = 118 if period_t0==6
replace period = 117 if period_t0==5
replace period = 116 if period_t0==4
replace period = 115 if period_t0==3
replace period = 114 if period_t0==2
replace period = 113 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==113
replace date = date_t0 - 1 if period==114
replace date = date_t0 - 1 if period==115
replace date = date_t0 - 1 if period==116
replace date = date_t0 - 1 if period==117
replace date = date_t0 - 1 if period==118
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period32 arrival depdur)
rename arrival arrival32
rename depdur depdur32
drop if _merge==2
replace period32=0 if period32==.

drop period date _merge


gen period = period_t0 - 33
replace period = 144 if period_t0==33
replace period = 143 if period_t0==32
replace period = 142 if period_t0==31
replace period = 141 if period_t0==30
replace period = 140 if period_t0==29
replace period = 139 if period_t0==28
replace period = 138 if period_t0==27
replace period = 137 if period_t0==26
replace period = 136 if period_t0==25
replace period = 135 if period_t0==24
replace period = 134 if period_t0==23
replace period = 133 if period_t0==22
replace period = 132 if period_t0==21
replace period = 131 if period_t0==20
replace period = 130 if period_t0==19
replace period = 129 if period_t0==18
replace period = 128 if period_t0==17
replace period = 127 if period_t0==16
replace period = 126 if period_t0==15
replace period = 125 if period_t0==14
replace period = 124 if period_t0==13
replace period = 123 if period_t0==12
replace period = 122 if period_t0==11
replace period = 121 if period_t0==10
replace period = 120 if period_t0==9
replace period = 119 if period_t0==8
replace period = 118 if period_t0==7
replace period = 117 if period_t0==6
replace period = 116 if period_t0==5
replace period = 115 if period_t0==4
replace period = 114 if period_t0==3
replace period = 113 if period_t0==2
replace period = 112 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==112
replace date = date_t0 - 1 if period==113
replace date = date_t0 - 1 if period==114
replace date = date_t0 - 1 if period==115
replace date = date_t0 - 1 if period==116
replace date = date_t0 - 1 if period==117
replace date = date_t0 - 1 if period==118
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period33 arrival depdur)
rename arrival arrival33
rename depdur depdur33
drop if _merge==2
replace period33=0 if period33==.

drop period date _merge


gen period = period_t0 - 34
replace period = 144 if period_t0==34
replace period = 143 if period_t0==33
replace period = 142 if period_t0==32
replace period = 141 if period_t0==31
replace period = 140 if period_t0==30
replace period = 139 if period_t0==29
replace period = 138 if period_t0==28
replace period = 137 if period_t0==27
replace period = 136 if period_t0==26
replace period = 135 if period_t0==25
replace period = 134 if period_t0==24
replace period = 133 if period_t0==23
replace period = 132 if period_t0==22
replace period = 131 if period_t0==21
replace period = 130 if period_t0==20
replace period = 129 if period_t0==19
replace period = 128 if period_t0==18
replace period = 127 if period_t0==17
replace period = 126 if period_t0==16
replace period = 125 if period_t0==15
replace period = 124 if period_t0==14
replace period = 123 if period_t0==13
replace period = 122 if period_t0==12
replace period = 121 if period_t0==11
replace period = 120 if period_t0==10
replace period = 119 if period_t0==9
replace period = 118 if period_t0==8
replace period = 117 if period_t0==7
replace period = 116 if period_t0==6
replace period = 115 if period_t0==5
replace period = 114 if period_t0==4
replace period = 113 if period_t0==3
replace period = 112 if period_t0==2
replace period = 111 if period_t0==1

gen date = date_t0
replace date = date_t0 - 1 if period==111
replace date = date_t0 - 1 if period==112
replace date = date_t0 - 1 if period==113
replace date = date_t0 - 1 if period==114
replace date = date_t0 - 1 if period==115
replace date = date_t0 - 1 if period==116
replace date = date_t0 - 1 if period==117
replace date = date_t0 - 1 if period==118
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period34 arrival depdur)
rename arrival arrival34
rename depdur depdur34
drop if _merge==2
replace period34=0 if period34==.

drop period date _merge


gen period = period_t0 - 35
replace period = 144 if period_t0==35
replace period = 143 if period_t0==34
replace period = 142 if period_t0==33
replace period = 141 if period_t0==32
replace period = 140 if period_t0==31
replace period = 139 if period_t0==30
replace period = 138 if period_t0==29
replace period = 137 if period_t0==28
replace period = 136 if period_t0==27
replace period = 135 if period_t0==26
replace period = 134 if period_t0==25
replace period = 133 if period_t0==24
replace period = 132 if period_t0==23
replace period = 131 if period_t0==22
replace period = 130 if period_t0==21
replace period = 129 if period_t0==20
replace period = 128 if period_t0==19
replace period = 127 if period_t0==18
replace period = 126 if period_t0==17
replace period = 125 if period_t0==16
replace period = 124 if period_t0==15
replace period = 123 if period_t0==14
replace period = 122 if period_t0==13
replace period = 121 if period_t0==12
replace period = 120 if period_t0==11
replace period = 119 if period_t0==10
replace period = 118 if period_t0==9
replace period = 117 if period_t0==8
replace period = 116 if period_t0==7
replace period = 115 if period_t0==6
replace period = 114 if period_t0==5
replace period = 113 if period_t0==4
replace period = 112 if period_t0==3
replace period = 111 if period_t0==2
replace period = 110 if period_t0==1


gen date = date_t0
replace date = date_t0 - 1 if period==110
replace date = date_t0 - 1 if period==111
replace date = date_t0 - 1 if period==112
replace date = date_t0 - 1 if period==113
replace date = date_t0 - 1 if period==114
replace date = date_t0 - 1 if period==115
replace date = date_t0 - 1 if period==116
replace date = date_t0 - 1 if period==117
replace date = date_t0 - 1 if period==118
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period35 arrival depdur)
rename arrival arrival35
rename depdur depdur35
drop if _merge==2
replace period35=0 if period35==.

drop period date _merge


gen period = period_t0 - 36
replace period = 144 if period_t0==36
replace period = 143 if period_t0==35
replace period = 142 if period_t0==34
replace period = 141 if period_t0==33
replace period = 140 if period_t0==32
replace period = 139 if period_t0==31
replace period = 138 if period_t0==30
replace period = 137 if period_t0==29
replace period = 136 if period_t0==28
replace period = 135 if period_t0==27
replace period = 134 if period_t0==26
replace period = 133 if period_t0==25
replace period = 132 if period_t0==24
replace period = 131 if period_t0==23
replace period = 130 if period_t0==22
replace period = 129 if period_t0==21
replace period = 128 if period_t0==20
replace period = 127 if period_t0==19
replace period = 126 if period_t0==18
replace period = 125 if period_t0==17
replace period = 124 if period_t0==16
replace period = 123 if period_t0==15
replace period = 122 if period_t0==14
replace period = 121 if period_t0==13
replace period = 120 if period_t0==12
replace period = 119 if period_t0==11
replace period = 118 if period_t0==10
replace period = 117 if period_t0==9
replace period = 116 if period_t0==8
replace period = 115 if period_t0==7
replace period = 114 if period_t0==6
replace period = 113 if period_t0==5
replace period = 112 if period_t0==4
replace period = 111 if period_t0==3
replace period = 110 if period_t0==2
replace period = 109 if period_t0==1


gen date = date_t0
replace date = date_t0 - 1 if period==109
replace date = date_t0 - 1 if period==110
replace date = date_t0 - 1 if period==111
replace date = date_t0 - 1 if period==112
replace date = date_t0 - 1 if period==113
replace date = date_t0 - 1 if period==114
replace date = date_t0 - 1 if period==115
replace date = date_t0 - 1 if period==116
replace date = date_t0 - 1 if period==117
replace date = date_t0 - 1 if period==118
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period36 arrival depdur)
rename arrival arrival36
rename depdur depdur36
drop if _merge==2
replace period36=0 if period36==.

drop period date _merge

gen period = period_t0 - 37
replace period = 144 if period_t0==37
replace period = 143 if period_t0==36
replace period = 142 if period_t0==35
replace period = 141 if period_t0==34
replace period = 140 if period_t0==33
replace period = 139 if period_t0==32
replace period = 138 if period_t0==31
replace period = 137 if period_t0==30
replace period = 136 if period_t0==29
replace period = 135 if period_t0==28
replace period = 134 if period_t0==27
replace period = 133 if period_t0==26
replace period = 132 if period_t0==25
replace period = 131 if period_t0==24
replace period = 130 if period_t0==23
replace period = 129 if period_t0==22
replace period = 128 if period_t0==21
replace period = 127 if period_t0==20
replace period = 126 if period_t0==19
replace period = 125 if period_t0==18
replace period = 124 if period_t0==17
replace period = 123 if period_t0==16
replace period = 122 if period_t0==15
replace period = 121 if period_t0==14
replace period = 120 if period_t0==13
replace period = 119 if period_t0==12
replace period = 118 if period_t0==11
replace period = 117 if period_t0==10
replace period = 116 if period_t0==9
replace period = 115 if period_t0==8
replace period = 114 if period_t0==7
replace period = 113 if period_t0==6
replace period = 112 if period_t0==5
replace period = 111 if period_t0==4
replace period = 110 if period_t0==3
replace period = 109 if period_t0==2
replace period = 108 if period_t0==1


gen date = date_t0
replace date = date_t0 - 1 if period==108
replace date = date_t0 - 1 if period==109
replace date = date_t0 - 1 if period==110
replace date = date_t0 - 1 if period==111
replace date = date_t0 - 1 if period==112
replace date = date_t0 - 1 if period==113
replace date = date_t0 - 1 if period==114
replace date = date_t0 - 1 if period==115
replace date = date_t0 - 1 if period==116
replace date = date_t0 - 1 if period==117
replace date = date_t0 - 1 if period==118
replace date = date_t0 - 1 if period==119
replace date = date_t0 - 1 if period==120
replace date = date_t0 - 1 if period==121
replace date = date_t0 - 1 if period==122
replace date = date_t0 - 1 if period==123
replace date = date_t0 - 1 if period==124
replace date = date_t0 - 1 if period==125
replace date = date_t0 - 1 if period==126
replace date = date_t0 - 1 if period==127
replace date = date_t0 - 1 if period==128
replace date = date_t0 - 1 if period==129
replace date = date_t0 - 1 if period==130
replace date = date_t0 - 1 if period==131
replace date = date_t0 - 1 if period==132
replace date = date_t0 - 1 if period==133
replace date = date_t0 - 1 if period==134
replace date = date_t0 - 1 if period==135
replace date = date_t0 - 1 if period==136
replace date = date_t0 - 1 if period==137
replace date = date_t0 - 1 if period==138
replace date = date_t0 - 1 if period==139
replace date = date_t0 - 1 if period==140
replace date = date_t0 - 1 if period==141
replace date = date_t0 - 1 if period==142
replace date = date_t0 - 1 if period==143
replace date = date_t0 - 1 if period==144

merge 1:1 period date site_code using "$saveddata/temp_panel_aearrivalsandexits_gs_10mins.dta", keepusing(period37 arrival depdur)
rename arrival arrival37
rename depdur depdur37
drop if _merge==2
replace period37=0 if period37==.

drop period date _merge

gen wait3_6 = period36+period35+period34+period33+period32+period31+period30+period29+period28+period27+period26+period25+period24+period23+period22+period21+period20+period19
gen total_wait = period37+period36+period35+period34+period33+period32+period31+period30+period29+period28+period27+period26+period25+period24+period23+period22+period21+period20+period19+period18+period17+period16+period15+period14+period13+period12+period11+period10+period9+period8+period7+period6+period5+period4+period3+period2+period1

rename arrival_t0 arrival
rename depdur_t0 depdur
rename hour_t0 hour
rename period_t0 period
rename date_t0 date

forval x=1(1)36{
egen std_period`x' = std(period`x')
}

egen std_arrival = std(arrival)

gen dow = dow(date)
gen week = week(date)


gen period24_over = period24+period25+period26+period27+period28+period29+period30+period31+period32+period33+period34+period35+period36+period37
egen std_period24_over = std(period24_over)
egen std_period37 = std(period37)

gen finyear=2011 if date<19084
replace finyear = 2012 if date>19083

reghdfe depdur std_arrival std_period1 std_period2 std_period3 std_period4 std_period5 std_period6 std_period7 std_period8 std_period9 std_period10 std_period11 std_period12 std_period13 std_period14 std_period15 std_period16 std_period17 std_period18 std_period19 std_period20 std_period21 std_period22 std_period23 std_period24 std_period25 std_period26 std_period27 std_period28 std_period29 std_period30 std_period31 std_period32 std_period33 std_period34 std_period35 std_period36 std_period37, absorb(i.site_num#i.dow i.site_num#i.finyear i.site_num#i.week i.site_num#i.period) vce(cluster site_num) fast
outreg2 using "`results'/predicted_`start'`end'_results_diag_hosp.xls", replace keep(std_arrival std_period1 std_period2 std_period3 std_period4 std_period5 std_period6 std_period7 std_period8 std_period9 std_period10 std_period11 std_period12 std_period13 std_period14 std_period15 std_period16 std_period17 std_period18 std_period19 std_period20 std_period21 std_period22 std_period23 std_period24 std_period25 std_period26 std_period27 std_period28 std_period29 std_period30 std_period31 std_period32 std_period33 std_period34 std_period35 std_period36 std_period37)

mat beta=e(b)
svmat double beta

gen se1 = _se[std_arrival]

forval x=2(1)38{
local y = `x'-1
gen se`x' = _se[std_period`y']
}

keep beta* se*
drop if beta1==. 

gen i = 1

reshape long beta se, i(i) j(coeff)

replace coeff = coeff-1
replace coeff = coeff*10

save "$saveddata/assumption_`start'`end'.dta", replace

}
